package com.cmpan.networkdisk.controller;

import com.cmpan.networkdisk.model.Disk;
import com.cmpan.networkdisk.model.Type;
import com.cmpan.networkdisk.model.User;
import com.cmpan.networkdisk.service.DiskService;
import com.cmpan.networkdisk.service.TypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.List;

/**
 * ClassName:DiskController
 * Package:com.cmpan.networkdisk.controller
 * Description:
 *
 * @Date:2020/7/7 21:51
 * @com.chuangmei
 */
@Controller
@RequestMapping("/disk")
public class DiskController {

    @Autowired
    private DiskService diskService;

    @Autowired
    private TypeService typeService;

    @GetMapping("/delete/{id}")
    public String deleteFile(@PathVariable String id, Model model){

        //Service删除
        Integer count = diskService.deleteById(Long.valueOf(id));

        if (count > 0){
            List<Disk> Alldisk = diskService.selectAll();
            model.addAttribute("disk",Alldisk);
        }

        return count > 0 ? "redirect:/goDisk" : "/error/error";
    }


    //此方法受登录拦截
    @GetMapping("/search")
    public String search(String[] fileName, Model model, HttpServletRequest request){

        System.out.println("进入搜索controller,参数: " + Arrays.toString(fileName));

        //掉用service层通过filename进行查询
        List<Disk> disks = diskService.deleteByFileName(fileName);
        List<Type> types = typeService.getAllType();
        Type type = new Type();
        //这个fileName就是搜索框提交的数据,因为之前是根据后缀名进行的分类查询,所以有接收数组参数,现在不用
        type.setTypeName(fileName[0]);
        //加入disk,和user到request
        //此时一定是已登录状态
        model.addAttribute("disk",disks);
        model.addAttribute("user",request.getSession().getAttribute("user"));
        model.addAttribute("types",types);
        model.addAttribute("type",type);
        return "search";
    }

    @GetMapping("/searchType")
    public String searchByType(String typeid,HttpServletRequest request,Model model){


        //调用service层通过typeid进行查找相同的文件
        List<Disk> disks = diskService.searchByTypeID(typeid);
        List<Type> types = typeService.getAllType();
        //通过id获取type
        Type type = typeService.queryByPrimary(Long.valueOf(typeid));

        model.addAttribute("disk",disks);
        model.addAttribute("user",request.getSession().getAttribute("user"));
        model.addAttribute("types",types);
        model.addAttribute("type",type);
        return "search";
    }


    @GetMapping("modify/{id}")
    public String modifyDisk(@PathVariable String id, HttpServletRequest request,Model model){

        //通过id查询disk
        Disk disk = diskService.selectDiskById(Long.valueOf(id));
        //查询出所有类型
        List<Type> types = typeService.getAllType();

        //加入request
        model.addAttribute("disk",disk);
        model.addAttribute("types",types);
        model.addAttribute("user",request.getSession().getAttribute("user"));
        return "admin/editorDisk";
    }

    @PostMapping("/modifydisk")
    public String modifyDisk(Disk disk ,HttpServletRequest request,Model model){

        int count = diskService.modifyByIdSelective(disk);

        return count > 0 ? "redirect:/goDisk" : "error/error";
    }

    @GetMapping("/batch")
    public String BatchOperation(HttpServletRequest request, Model model){

        //拿到user
        User user = (User) request.getSession().getAttribute("user");
        List disk = diskService.selectAll();
        model.addAttribute("disk",disk);
        model.addAttribute("user",user);
        return "admin/batchDisk";

    }

    @PostMapping("/deletemore")
    public @ResponseBody
    String deleteMore(String[] id, HttpServletRequest request){

        //删除
        StringBuffer stringBuffer = diskService.batchDelete(id);

        String message = stringBuffer.toString();

        System.out.println(message);

        return message;
    }

}
